<?php
namespace Home\Controller;
use Think\Controller;

/**
 *元器件分类控制器CatController
 */

class CatController extends Controller {
    protected $b_c_wzfl = null;
    public function __construct(){
        parent::__construct();
        $this->b_c_wzfl = D('b_c_wzfl');
    }

    //分类列表(包含二级分类和其最后一级分类)
    public function cats(){
        $redis = linkredis();
    	if($catinfo = $redis->get('catinfo')){
            $info = unserialize($catinfo);
        }else {
            $info = unserialize(file_get_contents('./Public/catinfo.txt'));
        }
        //dump($info);exit;
        $this->assign(array(
            'title' => '分类列表',
            'page_title' => '分类',  //选项卡名称
            'info' => $info
        ));
        $this->display();
    }

    //分类--产品页面
    public function cat_wzbm(){
        $a = microtime(true);
        $flno = I('get.flno');
        $b_c_wzbm = D('b_c_wzbm');
        $flmc = $this->b_c_wzfl->field('flmc')->find($flno);  //分类名称
        $wzbm_count = $b_c_wzbm->where("wzfl={$flno}")->count();
        //dump($wzbm_count);exit;
        if($wzbm_count > 50){
            $redis = linkredis();
            if ($data = $redis->get($flno.'_fl_wzbmfilter')) {
                $filterinfo = unserialize($data);
            } else {
                $filterinfo['sx'] = $this->b_c_wzfl->getcat_sx_filter($flno);
            }
        }
        //dump($filterinfo);exit;
        if($filterinfo['xh_gys']){  //型号状态
            $xh_type = true;
        }else{
            $xh_type = false;
        }
        if(!$filterinfo){
            $xh_type = true;
        }

        $data = $b_c_wzbm->getcatwzbm($flno,'wzfl'); //取出该分类下的单品信息
        //dump(microtime(true)-$a);
        //dump($data);exit;
        $this->assign(array(
            'count' => $wzbm_count,
            'info' => $data['info'],
            'tiaoshu' => $data['tiaoshu']
        ));

        $this->assign(array(
            'title' => '分类产品列表页',
            'page_title' => '分类',
            'flno' => $flno,
            'flmc' => $flmc['flmc'],
            'filterinfo' => $filterinfo, //属性,型号和供应筛选数据
            'xh_type' => $xh_type, //型号状态
        ));
        $this->display();
    }

    //页面加载完毕ajax获取型号和供应商的筛选数据
    public function ajax_xhgys_filter(){
        $flno = I('get.flno');
        $data = $this->b_c_wzfl->getcat_xhgys_filter($flno);
        $this->ajaxReturn($data);
    }

    //ajax获取分类下的更多产品
    public function ajax_getcatoffsetwxbm(){
        $flno = I('post.flno');
        $b_c_wzbm = D('b_c_wzbm');
        $data =$b_c_wzbm->getcatwzbm($flno,'wzfl');
        $this->ajaxReturn( array(
            'info' => $data['info'],
            'tiaoshu' => $data['tiaoshu']
        ));
    }
    //缓存分类脚本
    public function cachecatinfo(){
        $redis = linkredis();
        //获取二级分类
        $info = $this->b_c_wzfl->getcat();
        //dump($info);
        //循环二级分类取出最后一级分类
        foreach ($info as $k => &$v) {
            $v['lastcat'] = $this->b_c_wzfl->getcatlast($v['flbm']);
        }
        //数据存入redis数据库
        $redis->set('catinfo',serialize($info));
        $redis->expire('catinfo',3600*20);
    }


    public function filter_catwzbm(){
        //$a = microtime(true);
        $flno = I('get.flno');
        $flmc = I('get.flmc');
        $xh_gys = I('get.xh_gys');
        $sx = I('get.sx');
        //dump(I('get.'));exit;
        if($xh_gys || $sx) { //有筛选数据提交
            //取出筛选数据
            $redis = linkredis();
            if ($list = $redis->get($flno.'_fl_wzbmfilter')) {
                $filterinfo = unserialize($list);
            }else{
                $filterinfo = $this->b_c_wzfl->getwzflwzbmfilter($flno);
            }
            //dump($filterinfo);exit;

            /**-----------------------------把提交的筛选条件和分类ID拼接作为redis存储的键-------------------------------*/
            $key = '';
            foreach($xh_gys as $k=>$v){
                $key .= implode(',',$v).',';
            }
            foreach($sx as $k=>$v){
                $key .= implode(',',$v).',';
            }
            $key = $flno.'-'.$key;
            //dump($key);exit;
            $wznos = $redis->get($key);
            if($wznos){
                $wznoinfo = unserialize($wznos);
            }else{
                $wznoinfo = $this->b_c_wzfl->get_tj_catfilter($flno,$xh_gys,$sx);
                if ($wznoinfo) {
                    //把取得的物资ID存入redis,用上面拼接的字符串做键
                    $redis->set($key,serialize($wznoinfo));
                    $redis->expire($key,3600);
                    //通过物资ID取出相应条数的数据
                }
            }
            //dump($wznoinfo);exit;
            $data = [];
            if($wznoinfo){
                $data = $this->b_c_wzfl->get_wzfl_wzbm_filter($wznoinfo);
            }
            //dump($data);exit;

            $this->assign(array(
                'title' => '分类产品列表页',
                'page_title' => '分类',
                'flno' => $flno,
                'flmc' => $flmc,
                'filterinfo' => $filterinfo, //属性,型号和供应筛选数据
            ));
            $this->assign(array(
                'count' => count($wznoinfo),  //根据条件筛选取出数据的总条数
                'info' => $data['info'],  //根据条件筛选取出物资ID取出相应条数的数据
                'key' => $key,  //当前提交的条件拼接的redis的key键
                'offset' => $data['offset']  //已取数据的条数
            ));
            $this->display();
        } else{
            //如果没有筛选数据提交,重定向到cat_wzbm方法
            $this->redirect('cat_wzbm',array('flno'=>$flno));
        }
    }

    public function ajax_filter_getcatoffsetwxbm(){
        $key = I('post.key');
        $redis = linkredis();
        $wznoinfo = unserialize($redis->get($key));
        $data = $this->b_c_wzfl->get_wzfl_wzbm_filter($wznoinfo);
        $this->ajaxReturn($data);
    }


}